package com.aji.biomarker.controller;

import com.aji.allcommon.result.Result;
import com.aji.biomarker.config.FileEnvConfig;
import com.aji.biomarker.utils.FileUtils;
import com.aji.biomarker.domain.Correspondence;
import com.aji.biomarker.domain.dto.BiomarkerConditionDto;
import com.aji.biomarker.domain.vo.BiomarkerVo;
import com.aji.biomarker.service.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@CrossOrigin
@Api(tags = "生物标记物管理相关接口")
@RestController
@RequestMapping("/bio")
@RequiredArgsConstructor
public class Biocontroller {
    private final CorrespondenceService correspondenceService;
    private final BiomarkerService biomarkerService;
    private final FileEnvConfig fileEnvConfig;

    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Correspondence entity){
        correspondenceService.save(entity);
        return Result.success();
    }
//完成
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        correspondenceService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Correspondence entity){
        System.out.println(entity);
        correspondenceService.updateById(entity);
        return Result.success();
    }
//完成
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(biomarkerService.getById(id));
    }
//完成
    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list(){
        Page<BiomarkerVo> biomarkerVoPage = new Page<>(1,-1);
//        IPage<BiomarkerVo> biomarkerVoIPage = biomarkerService.listTakeName(biomarkerVoPage,new LambdaQueryWrapper<>());
        IPage<BiomarkerVo> biomarkerVoIPage = biomarkerService.page(biomarkerVoPage,new LambdaQueryWrapper<>());
        return Result.success(biomarkerVoIPage.getRecords());
    }
//完成
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, BiomarkerConditionDto biomarkerVo1){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<BiomarkerVo> biomarkerVoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<BiomarkerVo> biomarkerVoLambdaQueryWrapper =new LambdaQueryWrapper<BiomarkerVo>()
                .select()
                .eq(biomarkerVo1.getCompoundId()!=null,BiomarkerVo::getCompoundId,biomarkerVo1.getCompoundId())
                .eq(biomarkerVo1.getSpeciesId()!=null,BiomarkerVo::getSpeciesId,biomarkerVo1.getSpeciesId())
                .eq(biomarkerVo1.getReferenceId()!=null,BiomarkerVo::getReferenceId,biomarkerVo1.getReferenceId())
                .like(biomarkerVo1.getCname()!=null,BiomarkerVo::getCname,biomarkerVo1.getCname())
                .like(biomarkerVo1.getEname()!=null,BiomarkerVo::getEname,biomarkerVo1.getEname())
                .like(biomarkerVo1.getFormula()!=null,BiomarkerVo::getFormula,biomarkerVo1.getFormula())
                .like(biomarkerVo1.getCas()!=null,BiomarkerVo::getCas,biomarkerVo1.getCas())
                .like(biomarkerVo1.getReference()!=null,BiomarkerVo::getReference,biomarkerVo1.getReference())
                .like(biomarkerVo1.getOriginCategory()!=null,BiomarkerVo::getOriginCategory,biomarkerVo1.getOriginCategory())
                .like(biomarkerVo1.getOrigin()!=null,BiomarkerVo::getOrigin,biomarkerVo1.getOrigin())
                .like(biomarkerVo1.getRemark()!=null,BiomarkerVo::getRemark,biomarkerVo1.getRemark())
                .ge(biomarkerVo1.getWeightMin()!=null,BiomarkerVo::getWeight,biomarkerVo1.getWeightMin())
                .lt(biomarkerVo1.getWeightMax()!=null,BiomarkerVo::getWeight,biomarkerVo1.getWeightMax());
        IPage<BiomarkerVo> biomarkerVoIPage = biomarkerService.page(biomarkerVoPage,biomarkerVoLambdaQueryWrapper);
        return Result.success(biomarkerVoIPage);
    }

    @PutMapping("/upload")
    @ApiOperation("上传文件导入")
    @Transactional
    public Result upload(@RequestParam("file") MultipartFile file){
        return biomarkerService.importCsv(file);
    }

    @ApiOperation("获取 demo文件")
    @GetMapping("/demoFile")
    public ResponseEntity<byte[]> getDemoFile() throws IOException {
        // 拼接文件路径
        String fullPath = fileEnvConfig.getDataPath() + fileEnvConfig.getDemoFile();
        String fileName = fileEnvConfig.getDemoFile();

        // 使用工具类读取文件为 byte[]
        return FileUtils.transformFile(fullPath, fileName);
    }
}
